package imageProcessing.filter;

import imageProcessing.Duplicator;
import imageProcessing.ImageEvent;
import imageProcessing.ImageListener;

import java.awt.image.renderable.ParameterBlock;
import java.util.ArrayList;

import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.PlanarImage;

public class LaplaceFilter implements ImageListener {
	float[] _laplaceMatrix = new float[]{
			0, -1, 0,
			-1, 4, -1,			
			0, -1, 0,
	};
	
	private ArrayList<ImageListener> _listeners;
	private PlanarImage _image;
	
	public LaplaceFilter(){
		_listeners = new ArrayList<ImageListener>();
		_image = null;
	}

	public void notifyListeners() {
			ImageEvent ie  = new ImageEvent(this, _image);		
			ArrayList<ImageListener> copy = Duplicator.copyListeners(_listeners);
			for (ImageListener listener : copy) {
				listener.imageChanged(ie);
			}
	}
		
	public void addImageListener(ImageListener imageListener){
		_listeners.add(imageListener);
	}
	
	public void removeImageListener(ImageListener imageListener){
		_listeners.remove(imageListener);
	}
	@Override
	public void imageChanged(ImageEvent ie) {	
		PlanarImage img = ie.getImage();
		KernelJAI kernel = new KernelJAI(3, 3, _laplaceMatrix);
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(img);
    	pb.add(kernel);
    	_image = JAI.create("convolve", pb);
		notifyListeners();
	}
}
